package com.lyj.system.service.question.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lyj.common.core.enums.ResultCode;
import com.github.pagehelper.PageHelper;
import com.lyj.security.exception.ServiceException;
import com.lyj.system.domain.question.Question;
import com.lyj.system.domain.question.dto.QuestionAddDTO;
import com.lyj.system.domain.question.dto.QuestionEditDTO;
import com.lyj.system.domain.question.dto.QuestionQueryDTO;
import com.lyj.system.domain.question.vo.QuestionDetailVO;
import com.lyj.system.domain.question.vo.QuestionVO;
import com.lyj.system.mapper.question.QuestionMapper;
import com.lyj.system.service.question.QuestionService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LIUYANJIAO
 * Date: 2025-08-17
 * Time: 22:46
 */
@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public List<?> list(QuestionQueryDTO questionQueryDTO) {
        //TODO
        PageHelper.startPage(questionQueryDTO.getPageNum(),questionQueryDTO.getPageSize());
        List<QuestionVO> questionVOS = questionMapper.selectQuestionList(questionQueryDTO);
        return questionVOS;
    }

    @Override
    public int add(QuestionAddDTO questionAddDTO) {
        //判断题目是否存在（这里题目一样，就表示已存在）
        List<Question> questions = questionMapper.selectList(new QueryWrapper<Question>().lambda().eq(Question::getTitle, questionAddDTO.getTitle()));
        if(CollectionUtil.isNotEmpty(questions)){
            //已存在
            throw new ServiceException(ResultCode.FAILED_ALREADY_EXISTS);
        }
        Question question = new Question();
        BeanUtils.copyProperties(questionAddDTO,question);
        return questionMapper.insert(question);
    }

    @Override
    public QuestionDetailVO detail(Long questionId) {
        Question question = getQuestion(questionId);
        QuestionDetailVO questionDetailVO = new QuestionDetailVO();
        BeanUtils.copyProperties(question,questionDetailVO);
        return questionDetailVO;
    }



    @Override
    public int edit(QuestionEditDTO questionEditDTO) {
        //首先检查是否存在需修改对象
        Question question = getQuestion(questionEditDTO.getQuestionId());
        question.setTitle(questionEditDTO.getTitle());
        question.setDifficulty(questionEditDTO.getDifficulty());
        question.setTimeLimit(questionEditDTO.getTimeLimit());
        question.setSpaceLimit(questionEditDTO.getSpaceLimit());
        question.setContent(questionEditDTO.getContent());
        question.setQuestionCase(questionEditDTO.getQuestionCase());
        question.setDefaultCode(questionEditDTO.getDefaultCode());
        question.setMainFuc(questionEditDTO.getMainFuc());
        return questionMapper.updateById(question);
    }

    @Override
    public int delete(Long questionId) {
        Question question = getQuestion(questionId);
        return questionMapper.deleteById(questionId);
    }

    private Question getQuestion(Long questionId) {
        Question question = questionMapper.selectById(questionId);
        if(question == null){
            throw new ServiceException(ResultCode.FAILED_NOT_EXISTS);
        }
        return question;
    }
}
